package com.woniu.service.impl;

import com.woniu.entity.InpatientRecord;
import com.woniu.entity.MedSurgeryApplication;
import com.woniu.mapper.InpatientRecordMapper;
import com.woniu.mapper.MedSurgeryApplicationMapper;
import com.woniu.mapper.SurgeryItemMapper;
import com.woniu.service.ISurgeryAppService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import com.woniu.utils.UserContext;
import com.woniu.vo.SurgeryAppVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.List;

@Service
public class ISurgeryAppServiceImpl implements ISurgeryAppService {
    @Resource
    private MedSurgeryApplicationMapper mapper;
    @Resource
    private InpatientRecordMapper inpatientRecordMapper;
    @Resource
    private SurgeryItemMapper surgeryItemMapper;
    @Override
    public ResponseUtil listSurgeryApplications() {
        List<MedSurgeryApplication> list = mapper.listSurgeryApplications();
        return list.size() > 0 ?
                ResponseUtil.get(ResponseEnum.OK,list)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseUtil addRecord(MedSurgeryApplication record) throws ParseException {
        // 1 财务前置校验
        if (record.getSdSiId() == null || record.getSdCaseId() == null) {
            return ResponseUtil.get(ResponseEnum.FAIL, "缺少病人ID或手术项目ID");
        }
        // 2 获取手术价格
        BigDecimal surgeryPrice = surgeryItemMapper.getPriceById(record.getSdSiId());
        if (surgeryPrice == null) {
            return ResponseUtil.get(ResponseEnum.FAIL, "手术项目不存在或已下架");
        }

        // 3 获取患者当前住院押金余额
        InpatientRecord inpatientRecord = inpatientRecordMapper.findActiveRecordByPatientId(record.getSdCaseId()); //
        if (inpatientRecord == null) {
            return ResponseUtil.get(ResponseEnum.FAIL, "未找到该患者的在院记录");
        }
        BigDecimal currentBalance = inpatientRecord.getHzBalance();

        // 4 财务校验
        if (currentBalance.compareTo(surgeryPrice) < 0) {
            String msg = String.format("押金不足，无法申请！(当前余额: %.2f元, 手术费: %.2f元)",
                    currentBalance.doubleValue(),
                    surgeryPrice.doubleValue());
            return ResponseUtil.get(ResponseEnum.FAIL, msg);
        }

        // 获取医生id并赋值
        record.setSdWorkerId(UserContext.ContextUserId());
        Integer result = mapper.insertSelective(record);
        return result > 0 ?
                ResponseUtil.get(ResponseEnum.OK,record.getId())
                : ResponseUtil.get(ResponseEnum.FAIL);
    }
    @Override
    public ResponseUtil listSurgeryApplicationsByPatientId(Integer patientId) {
        List<SurgeryAppVO> list = mapper.listSurgeryApplicationsByPatientId(patientId);
        return ResponseUtil.get(ResponseEnum.OK, list);
    }

    @Override
    public ResponseUtil update(MedSurgeryApplication record) {
        Integer result = mapper.updateByPrimaryKeySelective(record);
        return result > 0 ? ResponseUtil.get(ResponseEnum.OK)
                : ResponseUtil.get(ResponseEnum.FAIL);
    }
}
